package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.common.vo.Result;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(version = "1.0",interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result save(List<OrderSetting> orderSettingList) {
        //如果重复上传 怎么办? 一个日期 只能有一个数据
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting db = orderSettingMapper.findOrderSettingByDate(orderSetting.getOrderDate());
            if (db == null) {
                orderSettingMapper.save(orderSetting);
            }else {
                db.setNumber(orderSetting.getNumber());
                orderSettingMapper.updateNumber(db);
            }
        }
        return Result.success(null);
    }
    //  this.leftobj = [
    //                        { date: 1, number: 120, reservations: 1 },
    //                        { date: 3, number: 120, reservations: 1 },
    //                        { date: 4, number: 120, reservations: 120 },
    //                        { date: 6, number: 120, reservations: 1 },
    //                        { date: 8, number: 120, reservations: 1 }
    //                    ];
    @Override
    public Result findByMonth(String month) {
        //2021-10
        //SELECT * FROM `t_ordersetting` where orderDate BETWEEN '2021-11-01' AND '2021-11-31'

        String start = month + "-01";
        String end = month + "-31";
        //mysql 8 不支持 这条sql SELECT * FROM `t_ordersetting` where DATE_FORMAT(orderDate,'%Y-%m') = '2021-11'
        List<OrderSetting> orderSettingList = this.orderSettingMapper.findOrderSettingByMonth(start,end);

        List<Map<String,Integer>> list = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {

            Map<String,Integer> map = new HashMap<>();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            list.add(map);
        }
        return Result.success(list);
    }

    @Override
    public Result updateNumber(String date,Integer number) {
        /**
         * 根据日期 进行更新，日期万一数据库没有 怎么办？没有新增
         */
        Date orderDate = null;
        try {
            orderDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail();
        }
        OrderSetting orderSetting = this.orderSettingMapper.findOrderSettingByDate(orderDate);
        if (orderSetting == null){
            orderSetting = new OrderSetting();
            orderSetting.setNumber(number);
            orderSetting.setOrderDate(orderDate);
            orderSetting.setReservations(0);
            this.orderSettingMapper.save(orderSetting);
        }else{
            orderSetting.setNumber(number);
            this.orderSettingMapper.updateNumber(orderSetting);
        }
        return Result.success(null);
    }
}
